#include <memory>
#include "Common.hpp"
#include "Http.hpp"
#include "Util.hpp"


//这里就简单模拟一下就好了，直接以txt文本返回
void Login(HttpRequest& req, HttpResponse& resp){
    //此时，不知道参数在正文还是在uri上
    //但是，在HttpRequest类内，用参数_args保存了！
    std::string args = req.GetArgs() == "" ? req.GetText() : req.GetArgs();
    resp.SetCodeAndDesc(200);

    //正文部分
    std::string resp_text = "haha, your username and password is looked : " + args;
    resp.SetText(resp_text);

    //设置报头 
    resp.SetHeaders("Content-Length", std::to_string(resp_text.size()));
    resp.SetHeaders("Content-Type", MimeTypes().getMimeType(".txt"));
}

void Register(HttpRequest& req, HttpResponse& resp){
    std::string args = req.GetArgs() == "" ? req.GetText() : req.GetArgs();
    resp.SetCodeAndDesc(200);

    std::string resp_text = "register : " + args;
    resp.SetText(resp_text);

    resp.SetHeaders("Content-Length", std::to_string(resp_text.size()));
    resp.SetHeaders("Content-Type", MimeTypes().getMimeType(".txt"));
}

//用来启动服务器的
// ./myhttp.exe server_port
int main(int argc, char* argv[]){
    if(argc != 2){
        std::cout << "please usage : ./myhttp.exe server_port" << std::endl;
        exit(USAGE_ERROR);
    }

    uint16_t server_port = std::stoi(argv[1]);
    std::unique_ptr<Http> http_server = std::make_unique<Http>(server_port);

    http_server->HttpServerInit();
    http_server->RegisterRoute("/login", Login);
    http_server->RegisterRoute("/register", Register);
    http_server->HttpServerStart();
    return 0;
}